<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.simpleforum.model.Post" %>
<%@ page import="com.example.simpleforum.model.Reply" %>
<%@ page import="com.example.simpleforum.model.Forum" %>
<%@ page import="com.example.simpleforum.model.User" %>
<%@ page import="com.example.simpleforum.model.User.Role" %>
<%@ page import="com.example.simpleforum.dao.ForumAdminRelationDao" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
    <title>帖子详情 - 极简论坛</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
    <div class="actions">
        <a href="PostListServlet" class="btn">返回列表</a>
        <a href="index.jsp" class="btn">返回首页</a>
    </div>
    
    <% 
        Post post = (Post) request.getAttribute("post");
        if (post != null) {
            User currentUser = (User) session.getAttribute("user");
            boolean isAuthor = currentUser != null && currentUser.getId() == post.getUserId();
            
            // 判断当前用户是否是主管理员
            boolean isMainAdmin = currentUser != null && currentUser.getRole() == Role.main_admin;
            
            // 判断当前用户是否是板块管理员
            boolean isForumAdmin = false;
            if (currentUser != null && currentUser.getRole() == Role.forum_admin && post.getForumId() > 0) {
                ForumAdminRelationDao forumAdminDao = new ForumAdminRelationDao();
                isForumAdmin = forumAdminDao.isForumAdmin(post.getForumId(), currentUser.getId());
            }
            
            // 判断当前用户是否有权限删除帖子
            boolean canDeletePost = isAuthor || isMainAdmin || isForumAdmin;
    %>
    <div class="post">
        <h1><%= post.getTitle() %></h1>
        <div class="post-meta">
            <% Forum forum = (Forum)request.getAttribute("forum"); 
               if (forum != null) { %>
            <span>所属板块: 
                <a href="PostListServlet?forumId=<%= forum.getId() %>">
                    <%= forum.getName() %>
                </a>
            </span>
            <% } %>
            <span>作者: <%= post.getUsername() %></span>
            <span>发布时间: <%= post.getCreateTime() %></span>
            <% if (post.getUpdateTime().after(post.getCreateTime())) { %>
            <span>最后编辑: <%= post.getUpdateTime() %></span>
            <% } %>
        </div>
        <div class="post-content">
            <%= post.getContent() %>
        </div>
        <div class="post-actions">
            <% if (isAuthor) { %>
                <a href="PostEditServlet?id=<%= post.getId() %>" class="btn">编辑</a>
            <% } %>
            <% if (canDeletePost) { %>
                <a href="PostDeleteServlet?id=<%= post.getId() %>" class="btn" onclick="return confirm('确定要删除这篇帖子吗？')">删除</a>
            <% } %>
        </div>
    </div>
    
    <div class="reply-form">
        <h2>发表回复</h2>
        <% if (currentUser != null) { %>
        <form action="ReplyAddServlet" method="post">
            <input type="hidden" name="postId" value="<%= post.getId() %>">
            <textarea name="content" required></textarea>
            <button type="submit">提交回复</button>
        </form>
        <% } else { %>
        <p>请<a href="login.jsp">登录</a>后发表回复</p>
        <% } %>
    </div>
    
    <div class="reply-list">
        <h2>回复列表</h2>
        <% 
            List<Reply> replies = (List<Reply>) request.getAttribute("replies");
            if (replies != null && !replies.isEmpty()) {
                for (Reply reply : replies) {
                    boolean isReplyAuthor = currentUser != null && currentUser.getId() == reply.getUserId();
                    
                    // 判断当前用户是否有权限删除回复
                    boolean canDeleteReply = isReplyAuthor || isMainAdmin || isForumAdmin;
        %>
        <div class="reply-item">
            <div class="reply-meta">
                <span>回复人: <%= reply.getUsername() %></span>
                <span>回复时间: <%= reply.getCreateTime() %></span>
                <% if (reply.getUpdateTime().after(reply.getCreateTime())) { %>
                <span>最后编辑: <%= reply.getUpdateTime() %></span>
                <% } %>
            </div>
            <div class="reply-content">
                <%= reply.getContent() %>
            </div>
            <div class="reply-actions">
                <% if (isReplyAuthor) { %>
                    <a href="ReplyEditServlet?id=<%= reply.getId() %>" class="btn">编辑</a>
                <% } %>
                <% if (canDeleteReply) { %>
                    <a href="ReplyDeleteServlet?id=<%= reply.getId() %>" class="btn" onclick="return confirm('确定要删除这条回复吗？')">删除</a>
                <% } %>
            </div>
        </div>
        <% 
                }
            } else {
        %>
        <p>暂无回复</p>
        <% } %>
    </div>
    <% } else { %>
    <p>帖子不存在或已被删除</p>
    <% } %>
</div>
</body>
</html>